/***********************************************************
* 
*     File Name:  subsrcibe.cpp
* 
*        Author:  Algolc
* 
*          mail:  17360224@qq.com
* 
*  Created Time:  Mon 09 Jul 2018 06:49:12 PM CST
* 
***********************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <sstream>
#include <zmq.h>
#include "log.h"

int main(int argc, char** argv) {
    if (argc < 2) {
        printf("Usage:%s [COUNT]", argv[0]);
        return -1;
    }

    std::stringstream ss;
    ss << "subAndpub_" << argv[1];

    init_log(ss.str().c_str());
    set_log_level(7);

    void* connect = zmq_ctx_new();  //创建新的环境
    assert(NULL != connect);
 
    int ret = zmq_ctx_set(connect, ZMQ_MAX_SOCKETS, 1);  //该环境中只允许有一个socket的存在
    assert(!ret);
 
    void* subsrciber = zmq_socket(connect, ZMQ_SUB);    //创建订阅者账号
    assert(NULL != subsrciber);
 
    ret = zmq_connect(subsrciber, "tcp://localhost:5556");
    assert(!ret);
 
    ret = zmq_setsockopt(subsrciber, ZMQ_SUBSCRIBE, "", 0); //不许添加该语句对消息进行滤波, 否则收不到消息
    assert(!ret);
 
    char buf[16];
    while(1) {
        ret = zmq_recv(subsrciber, buf, 16, ZMQ_DONTWAIT);
        if (-1 != ret) {
            //buf[ret] = '\0';
            log_debug("%s\n", buf);
        }
        usleep(300);
    }
 
    printf("test success");
    return 0;
}
